package cn.gailvlun.gll.presentation.login;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityLoginBinding;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.login.adapter.LoginType;
import cn.gailvlun.gll.presentation.login.bean.CountryCode;
import cn.gailvlun.gll.presentation.login.viewmodel.LoginViewModel;
import cn.gailvlun.gll.util.PhoneUtil;
import cn.gailvlun.gll.util.SPUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import cn.gailvlun.gll.widget.edittext.FilterType;



public class LoginActivity extends BaseActivity {

    private LoginViewModel mViewModel;
    private ActivityLoginBinding mBinding;

    @Deprecated
    public static void openActivity(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    public static void openActivity(Context context, String phoneNumber) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("phoneNumberWithCc", phoneNumber);
        context.startActivity(intent);
    }

    public static void openActivity(Context context, boolean clearOthers) {
        Intent intent = new Intent(context, LoginActivity.class);
        if (clearOthers) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        WelcomeActivity.openActivity(mContext, true);
    }

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        mViewModel = new LoginViewModel(mContext, mBinding);

        String phoneNumber = getIntent().getStringExtra("phoneNumberWithCc");
        if (phoneNumber != null) {
            mViewModel.getPhoneNumber().set(phoneNumber);
        }
        mBinding.setVm(mViewModel);

        CountryCode countryCode = SPUtil.get(SPUtil.GLOBAL, "countryCode", CountryCode.class);
        if (countryCode == null) {
            mViewModel.setCountryNameAndCode("CN +" + PhoneUtil.getCountryCodeByDefault());
        } else {
            int indexStart = countryCode.getCountryName().indexOf("(");
            int indexEnd = countryCode.getCountryName().indexOf(")");
            String text = countryCode.getCountryName().substring(indexStart + 1, indexEnd).trim() + " " + countryCode.getCountryCode();
            mViewModel.setCountryNameAndCode(text);
        }
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.petPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (!mBinding.tvLogin.isEnabled()) {
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    mViewModel.login();
                }
                return false;
            }
        });
        mBinding.cetPhone.startFilter(FilterType.PHONE);
        mBinding.petPassword.startFilter();
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        String loginTyp = SPUtils.getInstance().getString("loginType");
        if (!"".equals(loginTyp)) {
            LoginType loginType = LoginType.valueOf(loginTyp);
            switch (loginType) {
                case phone: {
                    
                    







                    break;
                }
                case qq: {
                    

                    break;
                }
                case wechat: {
                    

                    break;
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mViewModel.onActivityResult(requestCode, resultCode, data);
    }
}
